﻿using System.Data.Entity;
using System.Reflection;
using Abp.EntityFramework;
using Abp.Modules;
using AVIS.OutBound.EntityFramework;
using Abp.AutoMapper;
using AVIS.OutBound.EntityFramework.Models;
using AVIS.OutBound.Orders;
using AVIS.OutBound.Stores;

namespace AVIS.OutBound
{
    [DependsOn(typeof(AbpEntityFrameworkModule), typeof(OutBoundCoreModule))]
    public class OutBoundDataModule : AbpModule
    {
        public override void PreInitialize()
        {
            Configuration.DefaultNameOrConnectionString = "Default";

            // 配置对象映射的关系
            Configuration.Modules.AbpAutoMapper().Configurators.Add(config =>
            {
                //查询订单
                config.CreateMap<avisOBOrder, Order>()
                      .ForMember(u => u.CarGroup, options => options.Ignore()) // 忽略
                      .ForMember(u => u.SIPPCode, options => options.MapFrom(input => input.CarGroup))
                      .ForMember(u => u.Amount, options => options.MapFrom(input => input.QuotedTMI.Value))//订单金额
                      .ForMember(u => u.BookingDate, options => options.MapFrom(input => input.CreateDate))//下单时间
                      .ForMember(u => u.ReturnDateTime, options => options.MapFrom(input => input.ReturnDate))//还车时间
                      .ForMember(u => u.ReturnStoreCode, options => options.MapFrom(input => input.ReturnLoc))//还车门店编号
                      .ForMember(u => u.PickupDateTime, options => options.MapFrom(input => input.PickupDate))//取车时间
                      .ForMember(u => u.PickUpStoreCode, options => options.MapFrom(input => input.PickupLoc))//取车门店编号
                      .ForMember(u => u.RateCode, options => options.MapFrom(input => input.LastRateCode))//套餐价格代码
                      .ForMember(u => u.OrderNo, options => options.MapFrom(input => input.ReservationNumber));//订单号
                //保存订单
                config.CreateMap<Order, avisOBOrder>()
                      .ForMember(u => u.Id, options => options.Ignore())
                      .ForMember(u => u.CarGroup, options => options.MapFrom(input => input.SIPPCode))//预定车组唯一码
                      .ForMember(u => u.QuotedTMI, options => options.MapFrom(input => input.Amount))//订单金额
                      .ForMember(u => u.ReturnDate, options => options.MapFrom(input => input.ReturnDateTime))//还车时间
                      .ForMember(u => u.ReturnLoc, options => options.MapFrom(input => input.ReturnStoreCode))//还车门店编号
                      .ForMember(u => u.PickupDate, options => options.MapFrom(input => input.PickupDateTime))//取车时间
                      .ForMember(u => u.PickupLoc, options => options.MapFrom(input => input.PickUpStoreCode))//取车门店编号
                      .ForMember(u => u.LastRateCode, options => options.MapFrom(input => input.RateCode))//套餐价格代码
                      .ForMember(u => u.SpecialEquipmentDesc, options => options.MapFrom(input => input.EquipmentDesc))// 增值服务的描述
                      .ForMember(u => u.ReservationNumber, options => options.MapFrom(input => input.OrderNo));//订单号

                // 门店信息
                config.CreateMap<AvisORLocation, Store>()
                      .ForMember(u => u.Code, options => options.MapFrom(input => input.LocationCode.Trim()))//门店编号
                      .ForMember(u => u.CNName, options => options.MapFrom(input => input.LocationName.Trim()))//中文名称
                      .ForMember(u => u.ENName, options => options.MapFrom(input => input.LocationName.Trim()))//英文名称
                      .ForMember(u => u.RegionCode, options => options.MapFrom(input => input.RegionCode == null ? "" : input.RegionCode.Trim()))//地区编号
                      .ForMember(u => u.LocationAddress, options => options.MapFrom(input => input.LocationAddress == null ? "" : input.LocationAddress.Trim()))//门店地址
                      .ForMember(u => u.Telephone, options => options.MapFrom(input => input.Telephone == null ? "" : input.Telephone.Trim()))//传真
                      .ForMember(u => u.Fax, options => options.MapFrom(input => input.Fax == null ? "" : input.Fax.Trim()))//传真
                      .ForMember(u => u.CityCode, options => options.MapFrom(input => input.LocationCity == null ? "" : input.LocationCity.Trim()));//所属城市编号
            });
        }

        public override void Initialize()
        {
            IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly());
            Database.SetInitializer<OutBoundDbContext>(null);
        }
    }
}
